<?php
namespace app\admin\controller;

use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;

class Word{
public function exportWord()
{
    // 创建一个新的Word文档
    $phpWord = new PhpWord();
 
    // 添加一个新的空白页
    $section = $phpWord->addSection();
 
    // 添加文本到文档
    $section->addText('Hello, World!This is the file which be produced by PHP!');
 
    // 你可以添加更多的内容，比如表格、图片等
 
    // 写入Word文档到临时文件
    $temp_file = tempnam(sys_get_temp_dir(), 'PHPWord');
    IOFactory::createWriter($phpWord, 'Word2007')->save($temp_file);
    
    // 设置头部信息
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment;filename="' . basename($temp_file) . '"');
    header('Cache-Control: max-age=0');
 
    // 读取文件并直接输出到浏览器
    readfile($temp_file);
    unlink($temp_file); // 删除临时文件
    
    exit;
}
}